<?php
namespace console\controllers;

use common\models\Wxuser;
use Pheanstalk\Job;
use udokmeci\yii2beanstalk\BeanstalkController;
use Yii;
use yii\base\Exception;

class WorkerController extends BeanstalkController
{
    /**
     * @var integer 优先级 值越小优先级越高，默认为1024
     */
    const DELAY_PRIORITY = 1000;

    /**
     * @var integer 延迟执行的秒数
     */
    const DELAY_TIME = 5;

    /**
     * @var  integer 当 delay_max 达到 删除或延迟
     */
    const DELAY_MAX = 3;

    public function listenTubes()
    {
        return ['tube','wxdeal','tub','tubea','tubesetuser'];
    }

    /**
     * 采集数据
     * @param $job Job
     * @return string
     */
    public function actionTubea($job)
    {
        sleep(10);
        /** @var object $sentData */
        $sentData = $job->getData();
        if(isset($sentData->unique_id)&&$sentData->unique_id==11) {
            try{
                $user = Wxuser::findOne(['openid'=>'oEqJ_sxGJ_a-lQJPiH19Ctx96i0c']);
                if($user){
                    /** @var \callmez\wechat\sdk\Wechat $wechat $wechat */
                    $wechat = \Yii::$app->wechat;
                    $menberInfo = $wechat->getMemberInfo('oEqJ_sxGJ_a-lQJPiH19Ctx96i0c');
                    $user->is_gz=2;
                    $user->wx_images = $menberInfo['headimgurl'];
                    $user->wx_name = $menberInfo['nickname'];
                    $user->save();
//                    print_r($sentData->source[0]);
                }
            }catch (Exception $e){
                print_r($e->getMessage());
                return self::DELETE;
            }

        }
        // 不需要保存 mongodb 删除
        print_r($sentData);
        return self::DELETE;
    }

    /**
     * 采集数据
     * @param $job Job
     * @return string
     */
    public function actionTubesetuser($job)
    {
        sleep(10);
        /** @var object $sentData */
        $sentData = $job->getData();
        if(isset($sentData->unique_id)&&$sentData->unique_id==11) {
            try{
                $user = Wxuser::findOne(['openid'=>$sentData->source]);
                if($user){
                    /** @var \callmez\wechat\sdk\Wechat $wechat $wechat */
                    $wechat = \Yii::$app->wechat;
                    $menberInfo = $wechat->getMemberInfo($sentData->source);
                    $user->is_gz=2;
                    $user->wx_images = $menberInfo['headimgurl'];
                    $user->wx_name = $menberInfo['nickname'];
                    $user->save();
                }
            }catch (Exception $e){
                print_r($e->getMessage());
                return self::DELETE;
            }

        }
        // 不需要保存 mongodb 删除
        print_r('ok!!!');
        return self::DELETE;
    }

    /**
     * 采集数据
     * @param $job Job
     * @return string
     */
    public function actionWxdeal($job)
    {
        $sentData = $job->getData();
        print_r($sentData);
        // 不需要保存 mongodb 删除
        return self::DELETE;
    }

    /**
     * 采集数据
     * @param $job Job
     * @return string
     */
    public function actionTub($job)
    {
        $sentData = $job->getData();
        print_r($sentData);
        // 不需要保存 mongodb 删除
        return self::DELETE;
    }
}